package com.rd.student.jike.class2.lihongming.Solution713;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Auther:Heinrich
 * @Date:2023/7/13-07-13-10:07
 * @Description:com.rd.student.jike.class2.lihongming.Solution713
 * @version:1.0
 */
public class Question3 {

    public static int[][] generateMatrix(int n) {
        int toar = 0;
        int param = 1;
        int i=0,j=0;
        boolean flag = true;
        int[][] array = new int[n][n];
        while (flag) {
            if(i==toar&&j==toar){
                for ( j = toar; j < n-toar; j++) {
                    array[i][j]=param++;
                }
                j--;
            }
            if (i==toar&&j==n-1-toar) {
                i+=1;
                for (;  i< n-toar ; i++) {
                    array[i][j]=param++;
                }
                i--;

            }
            if (i==n-1-toar&&j==n-1-toar) {
                j-=1;
                for (;  j>=toar ; j--) {
                    array[i][j]=param++;
                }
                j++;
            }
            if (i==n-1-toar&&j==toar) {
                i-=1;
                for (; i > toar; i--) {
                    array[i][j]=param++;
                }

            }
            if(toar>=n)flag=false;
            i++;j++;toar++;
        }

        return array;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入矩阵边长");
        int n= sc.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(Question3.generateMatrix(n)[i][j]);
                System.out.print("\t");
            }
            System.out.println();
        }
    }
}
